package com.yazi.mxz.common.db.dao.impl;

import org.hibernate.Criteria;
import org.hibernate.criterion.LogicalExpression;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.yazi.mxz.common.db.dao.OrderDao;
import com.yazi.mxz.common.entity.beans.Member;
import com.yazi.mxz.common.entity.beans.Order;
import com.yazi.mxz.common.entity.beans.Store;
import com.yazi.mxz.common.entity.custom.Order4Search;
import com.myframework.smhj.hibernate.Searching;
import com.myframework.smhj.hibernate.Page;
import com.myframework.smhj.hibernate.dao.BasicDao;

@Repository("orderDao")
public class OrderDaoImpl extends BasicDao<Order> implements OrderDao {


	public OrderDaoImpl() {
		super(Order.class);
		
	}

	@Override
	public Criteria getExample(Order order, String searchWord) {
		Criteria c = super.getExample(order, searchWord);
		
		// 关联对象条件
		if (null != order) {
			// 用户
			Member member = order.getMemberByMemberId();
			if (null != member) {
				// 用户ID
				if (null != member.getMemberId()) {
					c.add(Restrictions.eq("memberByMemberId.memberId", member.getMemberId()));
				} else {
					// ...用户其它条件
				}
			}
			// 店铺
			Store store = order.getStore();
			if (null != store) {
				// 店铺ID
				if (null != store.getStoreId()) {
					c.add(Restrictions.eq("store.storeId", store.getStoreId()));
				} else {
					// ...店铺其它条件
				}
			}
			// ...其它关联对象条件
		}
		
		// ...关键字searchWord条件
		return c;
	}

	@Override
	public Page<Order> list(Order4Search order, Searching si) {
		Criteria c = getCriteriaForSearch(order, si.getSearchWord());
		return list(c, si);
	}
	
	@Override
	public int count(Order4Search order, String searchWord) {
		Criteria c = getCriteriaForSearch(order, searchWord);
		c.setCacheable(true);
		Object obj = c.setProjection(Projections.rowCount()).list().get(0);
		return Integer.parseInt(String.valueOf(obj));
	}
	
	private Criteria getCriteriaForSearch(Order4Search order, String searchWord) {
		Criteria c = getExample(order, searchWord);
		// 多种状态
		if (null != order.getStatuses() && order.getStatuses().length > 0) {
			c.add(Restrictions.in("status", order.getStatuses()));
		}
		// 时间范围
		if (null != order.getStartTime() && null != order.getEndTime()) {
			c.add(Restrictions.between("orderTime", order.getStartTime(), order.getEndTime()));
	
		} // 大于等于某个时间 
		else if (null != order.getStartTime()) {
			LogicalExpression le = Restrictions.or(Restrictions.eq("orderTime", order.getStartTime()), Restrictions.gt("orderTime", order.getStartTime()));
			c.add(le);
			
		} // 小于某个时间
		else if (null != order.getEndTime()) {
			c.add(Restrictions.lt("orderTime", order.getEndTime()));
		}
		return c;
	}
	
	@Override
	public Order oneNextSuccess() {
		return unique("oneNextSuccessHql", null);
	}
	
	@Override
	public Order oneNextReturned() {
		return unique("oneNextReturnedHql", null);
	}
}
